#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using pii = pair<int, int>;
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
int m = 0, n = 0;
class Solution {
public:
    int bfs(vector<vector<char>>& maze, vector<vector<bool>>& sta, int i, int j)
    {
        queue<pii> q;
        int count = 0;
        q.push({ i,j });
        sta[i][j] = true;
        while (!q.empty())
        {
            count++;
            int cnt = q.size();
            while (cnt--)
            {
                int a = q.front().first;
                int b = q.front().second;
                q.pop();
                for (int k = 0;k < 4;k++)
                {
                    int x = a + dx[k], y = b + dy[k];
                    if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.' && sta[x][y] == false)
                    {
                        cout << "x=" << x << ",y=" << y << endl;
                        if (x == 0 || y == 0 || x == m - 1 || y == n - 1)
                        {
                            cout << "x=" << x << ",y=" << y << endl;
                            return count;
                        }
                        q.push({ x,y });
                        sta[x][y] = true;
                    }
                }
            }
        }
        return -1;
    }
    int nearestExit(vector<vector<char>>& maze, vector<int>& entrance)
    {
        m = maze.size();
        n = maze[0].size();
        vector<vector<bool>> sta(m, vector<bool>(n, false));
        return bfs(maze, sta, entrance[0], entrance[1]);
    }
};